package Homework.Date7_02;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 4:用 PreparedStatement 实现删除功能（表随意）
 * 
 * @author Y
 *
 */
public class Deletefunction {
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("驱动未找到!");
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.print("要删除的学生的学号:");
		long stuno = in.nextLong();
		System.out.print("要删除的学生的老家:");
		String fromcity = in.next();
		String ur1 = "jdbc:mysql://localhost:3306/test?useSSL=false";
		Connection con = null;
		PreparedStatement sta = null;
		int re = 0;
		try {
			con = DriverManager.getConnection(ur1, "root", "zzq11475@");
			String sql = "DELETE from s_student WHERE ";
			sql += "stuno=? or fromcity=?";
			sta = con.prepareStatement(sql);
			sta.setLong(1, stuno);
			sta.setString(2, fromcity);
			re = sta.executeUpdate();
			System.out.println("改变了" + re + "行数据");
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			try {
				if (sta != null) {
					sta.close();
				}
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			System.out.println("资源释放完成");
		}

	}

}
